<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;
class KaidianController extends Controller
{
    //index
    public function index()
    {
    	return view('home/kaidian/index');
    }
    //register
    public function sendEmail(Request $request)
    {
    	// dd($request -> all());
    	$data = $request -> except('_token');
    	Mail::send('emails.type',['data' =>$data],function($message)
            use($data){
    		$message -> subject('验证邮件');
    		$message -> to($data['email']);
    	});

    }
    //register
    public function register($email)
    {

    	return view('home.kaidian.business',['email'=>$email]);
    }
    //update
    public function ajaxload()
    {	
		$data = DB::table('cates') ->where('pid','0') ->get();
		// $jsonData = json_encode($data);
		if($data)
		{
			return response()->json($data);
		}
    }
    //ajaxsecond
    public function second()
    {
    	// $data =json_encode($_POST);
    	$cates = DB::table('cates') ->  where('pid',$_POST['id']) ->get();
    	// echo $cates;
    	return response()->json($cates);
    }
    //商家注册update
    public function update(Request $request)
    {
    	// 验证错误
    	$this -> validate($request,[

    		'username' => 'required',
    		'password' => 'required',
    		'repassword'=> 'required|same:password',
    		'phone'=>'required|max:11|min:11',
    		'id' => 'required',
    		'cateid' => 'required',
    		'provice' => 'required',
    		'city' => 'required',
    		'address' => 'required',

    		],[
    		'username.required'=>'用户名不能为空',
    		'password.required'=> '密码不能为空',
    		'repassword.required'=>'重复密码不能为空',
    		'repassword.same'=>'两次输入的密码不一致',
    		'phone.max'=> '手机号必须是11位',
    		'phone.min'=> '手机号必须是11位',
    		'phone.required'=> '手机号不能为空',
    		'id.required'=> '主分类不能为空',	
    		'cateid.required'=> '副分类不能为空',
    		'provice.required'=> '省份不能为空',
    		'city.required'=> '城市不能为空',
    		'address.required'=> '地址不能为空',
    		]);
    	$data = $request -> except('_token','repassword','id');
    	$res = DB::table('businesses') 
    		-> where('username',$data['username']) 
    		->where('phone',$data['phone'])
    		->where('email',$data['email'])
    		-> get();
    	if($res)
    	{
    		return back() -> with(['info' =>'用户名、手机号、邮箱重复，请更改后重试']);
    	}else
    	{
    		$provice = array('北京市','天津市','重庆市','河北省','广东省','上海市');
    		$city = array(
    					array('昌平区','海淀区','朝阳区'),
        				array('滨海区','武清县','南开区'),
        				array('重庆1','重庆2','重庆3'),
        				array('廊坊','石家庄','保定'),
        				array('佛山','东莞','广州'),
        				array('浦东','普陀区','虹桥')
        			);
    		$county = array(
    					array(	
    							array('北七家','回龙观','七里渠'),
    							array('海淀区1','海淀区2','西二旗'),
    							array('上地','四惠','朝阳1'),
    						),
    					array(
    							array('滨海1','滨海2','滨海3'),
    							array('武清1','武清2'),
    							array(),
    						),
    					array(
    							array('霸州','永清'),
    							array('桥东','桥西'),
    							array('安国','定州'),
    						)
    				);

            //处理图片
            //判断图片是否上传
            if($request -> hasFile('image'))
            {

                //判断图片是否有效
                if($request -> file('image') -> isValid())
                {
                    //获取扩展名
                    $suffix = $request -> file('image') -> getClientOriginalExtension();
                    //随机名称
                    $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                    //移动位置
                    $request -> file('image') -> move('./uploads',$fileName);
                    $img = '/uploads/'.$fileName;
                }
            }
    		$remember_token = $remember_token = rand(1000000,9999999);
    		$created_at = date('Y-m-d H:i:s');
    	    $updated_at = date('Y-m-d H:i:s');
            $password = Crypt::encrypt($data['password']);
        	$business_address = $provice[$data['provice']].$city[$data['provice']][$data['city']].$county[$data['provice']][$data['city']][$data['county']].$data['address'];
            $newData = $request -> except('_token','repassword','id','provice','city','county','address','image');
            $newData['remember_token'] = $remember_token;
            $newData['created_at'] = $created_at;
            $newData['updated_at'] = $updated_at;
            $newData['business_address'] = $business_address;
            $newData['password'] = $password;
            $newData['img'] = $img;
            $res = DB::table('businesses') -> insert($newData);
        	if($res)
        	{   
                $cate = DB::table('cates') 
                    -> where('id',$newData['cateid'])
                    -> first();
                $cates['name'] = $newData['username'];
                $cates['pid'] = $newData['cateid'];
                $cates['path'] = $cate ->path . ',' . $newData['cateid'];
                
                $res = DB::table('cates') -> insert($cates);
                
                if($res)
                {
                    return view('home.kaidian.index');
                }else
                {
                    return back() -> with(['info' => '注册错误，请稍后重试']);
                }
        		
        	}else
        	{
        		return back() -> with(['info' => '注册错误，请稍后重试']);
        	}

    	}
    }
}
